//
//  PYCapturedImageViewController.m
//  PictureYourself
//
//  Created by Daniel on 14/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PYCapturedImageViewController.h"

@implementation PYCapturedImageViewController

@synthesize leftSwipeRecognizer = leftSwipeRecognizer_;
@synthesize rightSwipeRecognizer = rightSwipeRecognizer_;
@synthesize panGestureRecognizer = panGestureRecognizer_;

@synthesize imageView1 = imageView1;
@synthesize imageView2 = imageView2;

//@synthesize controller1 = controller1_;
//@synthesize controller2 = controller2_;

static CGSize screenSize_;

- (id)initWithNibName:(NSString *)nibNameOrNil imageIndex:(int)index
{
    self = [self initWithNibName:nibNameOrNil bundle:nil];
    if (self) 
    {
        imageIndex_ = index;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    screenSize_ = [[UIScreen mainScreen] bounds].size;
    
    [self.view addGestureRecognizer:self.panGestureRecognizer];
    
    //self.controller1 = [[PYImageViewController alloc] initWithNibName:@"PYImageViewController" bundle:nil];
    //[self.controller1 setImage:[[PYCaptureSessionManager instance].images objectAtIndex:imageIndex_]];
    //[self.navigationController pushViewController:self.controller1 animated:TRUE];
    [self.view addSubview:self.imageView1];
    self.imageView1.image = [[PYCaptureSessionManager instance].images objectAtIndex:imageIndex_];
    [self.view addSubview:self.imageView2];
	// Do any additional setup after loading the view.
}

-(IBAction)doneWasClicked:(id)sender
{
    [self dismissModalViewControllerAnimated:TRUE];
}

/*-(IBAction)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
    NSString *swipeDirection;
    UIImageView *fromView, *toView;
    NSUInteger targetIndex;
    PYCaptureSessionManager *captureManager = [PYCaptureSessionManager instance];
    //UIViewAnimationOptions animationOption;
    //PYImageViewController *targetController;
    
    switch ([recognizer direction])
    {
        case UISwipeGestureRecognizerDirectionLeft:
            
            swipeDirection = @"Left";
            //animationOption = UIViewAnimationTransitionFlipFromRight;
            targetIndex = imageIndex_ == [captureManager.images count] - 1 ? 0 : imageIndex_ + 1;
            
            break;
            
        case UISwipeGestureRecognizerDirectionRight:

            targetIndex = imageIndex_ == 0 ? [captureManager.images count] - 1 : imageIndex_ - 1;
            //animationOption = UIViewAnimationTransitionFlipFromLeft;
            swipeDirection = @"Right";
            break;
            
        default:
            
            return;
            swipeDirection = @"Unsupported";
            break;
    }
    
    if((targetIndex & 1) == 1)
    {
        fromView = self.imageView1;
        toView = self.imageView2;
        
        //targetController = self.controller2;
    }
    else
    {
        fromView = self.imageView2;
        toView = self.imageView1;
        
        //targetController = self.controller1;
    }
    
    //[targetController setImage:[[captureManager images] objectAtIndex:targetIndex]];
    //[self.navigationController pushViewController:targetController animated:TRUE];
    toView.image = [[captureManager images] objectAtIndex:targetIndex];
    CATransition *t = [[CATransition alloc] init];
    t.duration = 1;
    t.type = kCATransitionPush;
    //t.delegate = self;
    [self.imageView1.layer addAnimation:t forKey:nil];
    [self.imageView2.layer addAnimation:t forKey:nil];
    self.imageView1.hidden = YES;
    self.imageView2.hidden = NO;
    NSLog(@"%@ swipe requested\n", swipeDirection);
}
*/

-(void)panWasRecognized:(id)sender
{
    if(self.panGestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        viewStartPosition_ = self.view.center;
        NSLog(@"View start position is %f, %f\n", viewStartPosition_.x, viewStartPosition_.y);
        //NSLog(@"First pan point: %f, %f\n", panPoint.x, panPoint.y);
    }
    
    PYCaptureSessionManager *captureManager = [PYCaptureSessionManager instance];
    NSUInteger targetImageIndex;
    //CGPoint otherViewPosition = self.imageView2.center;
    CGPoint otherViewCenter;
    // Pan forward (from right to left)
    if(self.view.center.x < screenSize_.width / 2)
    {
        targetImageIndex = imageIndex_ == 0 ? [captureManager.images count] - 1 : imageIndex_ - 1;
        otherViewCenter = CGPointMake(screenSize_.width / 2 + self.view.bounds.size.width + 10, self.view.center.y);
        //otherViewPosition.x = self.view.center.x + self.view.bounds.size.width + 10;
    }
    else // Pan backward
    {
        targetImageIndex = imageIndex_ == [captureManager.images count] - 1 ? 0 : imageIndex_ + 1;
        otherViewCenter = CGPointMake(screenSize_.width / 2 - self.view.bounds.size.width - 10, self.view.center.y);
        //otherViewPosition.x = self.view.center.x - self.view.bounds.size.width - 10;
    }
    
    self.imageView2.image = [[captureManager images] objectAtIndex:targetImageIndex];
    
    CGPoint panPoint = [self.panGestureRecognizer translationInView:self.view];
    CGPoint viewPosition = self.view.center;
    
    viewPosition.x = viewStartPosition_.x + panPoint.x;

    [self.view setCenter:viewPosition];
    [self.imageView2 setCenter:otherViewCenter];
    
    //NSLog(@"Panning at point %f, %f\n", panPoint.x, panPoint.y);
    //NSLog(@"Setting view positions. Mine = %f, %f. Other = %f, %f\n", viewPosition.x, viewPosition.y, otherViewPosition.x, otherViewPosition.y);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    NSLog(@"Captured image view controller was unloaded\n");
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
